package com.wlh.animation.demo;

import com.wlh.animation.R;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Property;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by weilh on 2018/10/15
 */
public class ListItemAnimAct1 extends Activity {

    float oriWidth, oriHeight;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item_anim1);

        findViewById(R.id.video_area).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View view = findViewById(R.id.long_poster_img);
                if (oriWidth == 0) {
                    oriWidth = view.getWidth();
                    oriHeight = view.getHeight();
                }
                float[] values;
                if (view.getWidth() == oriWidth) {
                    values = new float[]{1f, 0.46f};
                } else {
                    values = new float[]{0.46f, 1f};
                }
                ObjectAnimator widthAnim = ObjectAnimator.ofFloat(view, new Property<View, Float>(Float.class,
                        "scaleToOrigin") {
                    @Override
                    public void set(View object, Float value) {
                        ViewGroup.LayoutParams lp = object.getLayoutParams();
                        lp.width = (int) (oriWidth * value);
                        lp.height = (int) (oriHeight * value);
                        object.setLayoutParams(lp);
                    }

                    @Override
                    public Float get(View object) {
                        return object.getWidth() / oriWidth;
                    }
                }, values);
                widthAnim.setDuration(300);
                widthAnim.start();
            }
        });


    }
}
